package de.tud.binpacking.data;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Solution implements Comparable<Solution> {
	
	private Set<Batch> batches = new HashSet<Batch>();
	private double costs = 0;
	
	public Set<Batch> getBatches() {
		return batches;
	}
	
	public Solution(Collection<Batch> batches){
		for(Batch batch : batches){
			this.addBatch(batch);
		}
	}

	public Solution() {
		
	}

	public boolean addBatch(Batch batch) {
		if(batch == null){
			throw new NullPointerException("batch cannot be null");
		}
		if(batches.add(batch)){
			costs += batch.getPathLength();
			return true;
		}
		return false;
	}
	
	public boolean removeBatch(Batch batch) {
		if(batch == null){
			throw new NullPointerException("null cannot be removed");
		}
		if(batches.remove(batch)){
			costs -= batch.getPathLength();
			return true;
		}
		return false;
	}
	
	public double getCosts() {
		return costs;
	}
	
	public int getNoBatches() {
		return batches.size();
	}
	
	@Override
	public String toString() {
		if(batches.isEmpty()) {
			return "no batches";
		}
		
		String solution = "Solution: \n";
		for(Batch batch : batches){
			solution += batch + "\n";
		}
		return solution;
	}

	@Override
	public int compareTo(Solution o) {
		if(this.getCosts() > o.getCosts()){
			return 1;
		}else if(this.getCosts() < o.getCosts()) {
			return -1;
		}else {
			return 0;
		}
	}
	
	public Solution copy() {
		Solution solution = new Solution();
		for(Batch batch : this.batches){
				solution.batches.add(batch.copy());
		}
		solution.costs = this.costs;
		return solution;		
	}
	
	
}
